package com.buss.customer.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.core.controller.BaseController;
import com.buss.core.exception.WebException;
import com.buss.core.persistence.ResultList;
import com.buss.core.sysparam.SysStaticParam;
import com.buss.customer.model.CustomerAccount;
import com.buss.customer.service.CustomerAccountService;

@Controller
@RequestMapping("/admin/customeraccount/")
public class CustomerAccountController extends BaseController {

	@Autowired
	private CustomerAccountService accountService;
	
	@Override
	public ModelAndView listPage(String menuid) {
		return null;
	}

	@RequestMapping("{type}")
	public ModelAndView editPage(@PathVariable String type,Long id){
		ModelAndView view = new ModelAndView("customeraccount/edit");
		CustomerAccount account = new CustomerAccount();
		if(EDIT_PAGE.equals(type)){
			account = accountService.loadAccount(id);
		}else{
			account.setCustomerId(id);			
		}
		view.addObject("account", account);
		return view;
	}
	
	@RequestMapping("add")
	@ResponseBody
	public Map<String, Object> add(CustomerAccount account){
		accountService.saveAccount(account);
		return SysStaticParam.resultMessage("新增单位账号信息成功!");
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public Map<String,Object> edit(CustomerAccount account){
		accountService.editAccount(account);
		return SysStaticParam.resultMessage("修改单位账号信息成功!");
	}
	
	@Override
	@RequestMapping("delete")
	@ResponseBody
	public Map<String, Object> delete(Long[] ids) throws WebException {
		accountService.removeAccount(ids);
		return SysStaticParam.resultMessage("删除单位账号信息成功!");
	}

	@RequestMapping("list")
	@ResponseBody
	public Map<String, Object> list(Long customerId,String accountName,String accountNumber){
		ResultList<CustomerAccount> rs = accountService.queryAccount(customerId, accountName, accountNumber);
		Map<String, Object> map = SysStaticParam.resultMessage("获取单位账号信息成功!");
		map.put(SysStaticParam._RESULT, rs);
		return map;
	}
	
}
